package com.guli.ware.config;

import com.guli.common.constant.MqConstant;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author zzb
 * @Date 2024/4/21 22:19
 * @Version 1.0
 */
@Configuration
public class MqConfig {

    /*
    * 库存解锁队列
    * */
    @Bean
    public Queue stockReleaseQueue() {
        return new Queue(MqConstant.STOCK_RELEASE_QUEUE, true);
    }
    /*
    * 库存死信队列
    * */
    @Bean
    public Queue stockDelayQeuee() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", MqConstant.STOCK_EVENT_EXCHANGE); // 死信交换机名称
        args.put("x-dead-letter-routing-key", MqConstant.STOCK_RELEASE_ROUTING); // 死信路由键
        args.put("x-message-ttl", 120000); // 设置消息过期时间为 120 秒
        return new Queue(MqConstant.STOCK_DELAY_QUEUE, true, false, false, args);
    }
    /*
    * 库存交换机
    * */
    @Bean
    public Exchange stockEventExchange() {
        return new DirectExchange(MqConstant.STOCK_EVENT_EXCHANGE, true, false);
    }
    /*
    * 解库存队列与交换机的绑定
    * */
    @Bean
    public Binding stockReleaseBinding() {
        return new Binding
                (MqConstant.STOCK_RELEASE_QUEUE,
                        Binding.DestinationType.QUEUE,
                        MqConstant.STOCK_EVENT_EXCHANGE,
                        MqConstant.STOCK_RELEASE_ROUTING,
                        null);
    }
    /*
    * 死信队列与交换机的绑定
    * */
    @Bean
    public Binding stockDelayBinding() {
        return new Binding
                (MqConstant.STOCK_DELAY_QUEUE,
                        Binding.DestinationType.QUEUE,
                        MqConstant.STOCK_EVENT_EXCHANGE,
                        MqConstant.STOCK_DELAY_ROUTING,
                        null);
    }

}
